<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of candidate_requirements_controller
 *
 * @author wilson
 */
class OwnerRequirementscontroller extends AppController {

    //put your code here
    var $name = "OwnerRequirements";

    function index($evaluation_id) {

        $this->layout = "ajax";
        $this->OwnerRequirement->Requirement->recursive = -1;
        $call_id = 1;
        $requerimientos = $this->OwnerRequirement->Requirement->find(
                'all', array(
            'conditions' => array('Requirement.tipo' => 'Propietario', 'Requirement.call_id' => $call_id),
            'fields' => array('Requirement.id'),
            'recursive' => -1,
                ));

        foreach ($requerimientos as $req) {


            $r = $req['Requirement']['id'];


            $cont = $this->OwnerRequirement->find('count', array('recursive' => -1, 'conditions' => array('OwnerRequirement.requirement_id' => $req['Requirement']['id'], 'OwnerRequirement.owner_evaluation_id' => $evaluation_id)));
            if ($cont == 0) {

                $this->OwnerRequirement->query("INSERT INTO owner_requirements (owner_evaluation_id,requirement_id)values($evaluation_id,$r)");
            }
        }
        $requerimientos = $this->OwnerRequirement->find('all', array('recursive' => 0, 'conditions' => array('OwnerRequirement.owner_evaluation_id' => $evaluation_id), 'fields' => array('OwnerRequirement.id', 'Requirement.texto', 'OwnerRequirement.calificacion')));
        $this->set('requisitos', $requerimientos);
    }

    function edit($id) {
        $this->layout = "ajax";
        if (empty($this->data)) {

            $this->data = $this->OwnerRequirement->find('first', array('conditions' => array('OwnerRequirement.id' => $id), 'fields' => array('OwnerRequirement.id', 'OwnerRequirement.requirement_id', 'OwnerRequirement.owner_evaluation_id', 'OwnerRequirement.concepto', 'OwnerRequirement.calificacion', 'Requirement.texto')));
        } else {


            if ($this->OwnerRequirement->saveAll($this->data)) {

                $this->redirect(array('controller' => 'ownerRequirements', 'action' => 'index', $this->data['OwnerRequirement']['owner_evaluation_id']));

                $this->Session->setFlash('Registro guardado exitosamente','flash_custom');
            }
        }
    }

}

?>
